package com.oncliedisk.administrator.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.oncliedisk.administrator.DB.Dao;
import com.oncliedisk.administrator.adapter.ChildAdapter;
import com.oncliedisk.administrator.content.FileConfig;
import com.oncliedisk.administrator.content.OnlineConfig;
import com.oncliedisk.administrator.model.FileTaskModel;
import com.oncliedisk.administrator.onlinedisk.R;
import com.oncliedisk.administrator.utils.ExploerLoadSdFileTask;
import com.oncliedisk.administrator.utils.HttpUtils;
import com.ypy.eventbus.EventBus;

import java.io.File;
import java.util.List;

public class ShowImageActivity extends Activity {
    private GridView mGridView;
    private List<String> list;
    private ChildAdapter adapter;
    protected Button btn_upload;
    protected Button bt_selectall;
    protected TextView select_folder;
    protected ImageView iv_back;
    /**
     * 上传图片的目录
     */
    private String PHOTO_DIR = "/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_show_image);

        initView();
        setListener();
        initData();

    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        list = bundle.getStringArrayList("data");
        adapter = new ChildAdapter(this, list, mGridView, btn_upload);
        mGridView.setAdapter(adapter);

        String mDesPath = bundle.getString("mDesPath");
        if(mDesPath != null) {
            PHOTO_DIR = mDesPath;
            select_folder.setText("我的网盘" + PHOTO_DIR);
            Log.i("ShowImageActivity", mDesPath);

        }
    }

    private void setListener() {
        bt_selectall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.selectAll();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        btn_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//				Toast.makeText(ShowImageActivity.this, "点击了上传", Toast.LENGTH_LONG).show();
//                for (String picPath : selected) {
//                    File file = new File(picPath);
//                    UploadPicture upload = new UploadPicture(ShowImageActivity.this, mApi,
//                            PHOTO_DIR, file);
//                    upload.execute();
//                }
                List<String> selected = adapter.getSelectItems();
                if(!HttpUtils.isNetworkAvailable(ShowImageActivity.this)){
                    Toast.makeText(ShowImageActivity.this, "请检查网络状态是否可用!", Toast.LENGTH_SHORT).show();
                    return;
                }
                for (String picPath : selected) {
                    String substring = picPath.substring(picPath.lastIndexOf("/")+1);
                    Log.i("info", "执行插入");
                    FileTaskModel taskModel = new FileTaskModel();
                    taskModel.setFileName(substring);
                    taskModel.setPath(PHOTO_DIR);
                    taskModel.setLocalPath(picPath);
                    taskModel.setType(FileConfig.TYPE_IMG);
                    taskModel.setState(FileTaskModel.STATE_WAIT_DOWN);
                    taskModel.setTaskType(OnlineConfig.TYPE_UP);
                    File file = new File(picPath);
                    taskModel.setSize(ExploerLoadSdFileTask.formateFileSize(file.length()));
                    Dao.getInstance(ShowImageActivity.this).saveFileTask(taskModel);
                }
                //存储完成，执行提醒传输页面.
                Toast.makeText(ShowImageActivity.this, "已加入上传列表", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new String(OnlineConfig.TYPE_UP));

            }
        });
//        select_folder.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(ShowImageActivity.this, "选择上传到某个文件夹", Toast.LENGTH_LONG).show();
//            }
//        });
    }

    private void initView() {
        btn_upload = (Button) findViewById(R.id.btn_upload);
        select_folder = (TextView) findViewById(R.id.select_folder);
        bt_selectall = (Button) findViewById(R.id.bt_selectall);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        mGridView = (GridView) findViewById(R.id.child_grid);
    }
    @Override
    public void onBackPressed() {
//        Toast.makeText(this, "选中 " + adapter.getSelectItems().size() + " 条目", Toast.LENGTH_LONG).show();
//		super.onBackPressed();
        finish();
    }


}
